﻿using System.Linq;
using System.Web;
using ActivateYourGlutes.Model;
using ActivateYourGlutes.Model.Contracts;
using ActivateYourGlutes.Model.Providers;

namespace ActivateYourGlutes.Helpers
{
    public static class LocaleFactory
    {
        public static UserLocaleSettings GetLocale(HttpContextBase httpContext)
        {
            UserLocaleSettings locale;
            var provider = new InternalAccountProvider(new EntityModelFactory());
            // TODO: Need to get rid of this nasty call by moving the locale factory down into the model
            var account = provider.InternalCurrentAccount(httpContext.User);
            if (account != null)
            {
                locale = new UserLocaleSettings
                             {
                    DateFormat = account.LocalDateFormat,// provider.DateFormat(account),
                    UnitOfLength = new UnitOfLength { Description = account.UnitOfLength.Description, ToMetersMultiplier = account.UnitOfLength.ToMetersMultiplier, Unit = account.UnitOfLength.Unit, UnitOfLengthId = account.UnitOfLength.UnitOfLengthId },  //provider.UnitOfLength(account),
                    UnitOfWeight = new UnitOfWeight {Description = account.UnitOfWeight.Description, ToKgMultiplier = account.UnitOfWeight.ToKgMultiplier, Unit = account.UnitOfWeight.Unit, UnitOfWeightId = account.UnitOfWeight.UnitOfWeightId} //provider.UnitOfWeight(account)
                };
            }
            else
            {
                ILookupProvider lookupProvider = new LookupProvider(new EntityModelFactory());
                locale = new UserLocaleSettings
                             {
                    DateFormat = "MM-dd-yyyy",
                    UnitOfLength = lookupProvider.UnitsOfLength.Where(ul => ul.Unit == "\"").First(),
                    UnitOfWeight = lookupProvider.UnitsOfWeight.Where(uw => uw.Unit == "lbs").First()
                };
                // don't save to session state
            }
            return locale;
        }
    }
}
